﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;

namespace SurveyControl.Controls
{
    public class SurveyControl : Control
    {
        #region Properties

        #region Header

        public object Header
        {
            get { return (object)GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Header.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HeaderProperty =
            DependencyProperty.Register("Header", typeof(object), typeof(SurveyControl), new PropertyMetadata(""));

        #endregion

        #region Survey

        public Survey Survey
        {
            get { return (Survey)GetValue(SurveyProperty); }
            set 
            { 
                SetValue(SurveyProperty, value);
                FillQuestion();
            }
        }

        // Using a DependencyProperty as the backing store for Survey.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SurveyProperty =
            DependencyProperty.Register("Survey", typeof(Survey), typeof(SurveyControl), new PropertyMetadata(new Survey(), new PropertyChangedCallback(OnSurveyChanged)));

        #endregion

        #region CurrentQuestion

        public object CurrentQuestion
        {
            get { return (object)GetValue(CurrentQuestionProperty); }
            set { SetValue(CurrentQuestionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CurrentQuestion.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CurrentQuestionProperty =
            DependencyProperty.Register("CurrentQuestion", typeof(object), typeof(SurveyControl), new PropertyMetadata(null));

        #endregion

        #region QuestionSource

        public IEnumerable<QuestionControl> QuestionsSource
        {
            get { return (IEnumerable<QuestionControl>)GetValue(QuestionsSourceProperty); }
            set 
            { 
                SetValue(QuestionsSourceProperty, value);
                FillQuestion();
            }
        }

        // Using a DependencyProperty as the backing store for QuestionsSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty QuestionsSourceProperty =
            DependencyProperty.Register("QuestionsSource", typeof(IEnumerable<QuestionControl>), typeof(SurveyControl), new PropertyMetadata(null, new PropertyChangedCallback(OnQuestionsSourceChanged)));

        #endregion

        #endregion

        #region Style

        #region NavigationStyle



        public Style NavigationStyle
        {
            get { return (Style)GetValue(NavigationStyleProperty); }
            set { SetValue(NavigationStyleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for NavigationStyle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NavigationStyleProperty =
            DependencyProperty.Register("NavigationStyle", typeof(Style), typeof(SurveyControl), new PropertyMetadata(null));

        

        #endregion

        #endregion

        public SurveyControl()
        {
            this.DefaultStyleKey = typeof(SurveyControl);
            this.Survey.PropertyChanged += this.onCurrentQuestionChanged;
        }

        private void FillQuestion ()
        {
            if (Survey == null || QuestionsSource == null)
                return;

            if (QuestionsSource is IEnumerable<QuestionControl>)
                Survey.Questions = new ObservableCollection<QuestionControl>(QuestionsSource);
        }

        #region Event Handler

        private static void OnSurveyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.OldValue is Survey)
                (args.OldValue as Survey).PropertyChanged -= (sender as SurveyControl).onCurrentQuestionChanged;
            if (args.NewValue is Survey)
                (args.NewValue as Survey).PropertyChanged += (sender as SurveyControl).onCurrentQuestionChanged;

            (sender as SurveyControl).FillQuestion();
        }

        private static void OnQuestionsSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            (sender as SurveyControl).FillQuestion();
        }

        private void onCurrentQuestionChanged(object sender, PropertyChangedEventArgs args)
        {
            if (args.PropertyName != Survey.CurrentQuestionPropertyName)
                return;
            if (Survey == null)
                return;
            CurrentQuestion = Survey.CurrentQuestion;
        }

        #endregion
    }
}
